using System;
using System.Web;
using System.Globalization;
using ODM2KMZ.Properties;


namespace ODM2KMZ
{
    public partial class DataStreamers_WaterOneFlowTableStreamer : System.Web.UI.Page
    {
        public struct MetaData
        {
            public string SiteName;
            public string SiteCode;
            public string Network;
            public string GeneralCatagory;
            public string VariableCode;
            public string VariableName;
            public string VariableDescription;
            public int ValueCount;
            public string State;
            public string County;
            public string BeginDate;
            public string EndDate;
        }
        public MetaData CurrentMetaData; // = new MetaData;
        public string CurrentURL
        {
            get
            {
                return Request.QueryString["URL"] == null ? "http://wron.net.au/WronOD/cuahsi_1_0.asmx" : Request.QueryString["URL"];
            }
        }
        public string Mode
        {
            get
            {
                return Request.QueryString["Mode"] == null ? "0" : Request.QueryString["Mode"];
            }
        }
        public string SiteCode
        {
            get
            {
                return Request.QueryString["SiteCode"] == null ? string.Empty : Request.QueryString["SiteCode"];
            }

        }
        public string strPath
        {
            get
            {
                string URL = Request.Url.AbsoluteUri.Contains("?") ? Request.Url.AbsoluteUri.Remove(Request.Url.AbsoluteUri.IndexOf("?")) : Request.Url.AbsoluteUri;
                URL = URL.Replace(Request.Url.AbsolutePath, "") + Request.ApplicationPath;


                return URL;
            }
        }
        public string strPathFake
        {
            get
            {
                return Request.Url.GetLeftPart(System.UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath + "/";

            }
        }
        public string VariableCode
        {
            get
            {
                return Request.QueryString["VariableCode"] == null ? string.Empty : Request.QueryString["VariableCode"];
            }

        }
        public string BeginDate
        {
            get
            {
                return Request.QueryString["BeginDate"] == null ? string.Empty : Request.QueryString["BeginDate"];
            }

        }
        public string EndDate
        {
            get
            {
                return Request.QueryString["EndDate"] == null ? string.Empty : Request.QueryString["EndDate"];
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            imgChart.ImageUrl = strPathFake + "WaterOneFlowChartStreamer.aspx" + Request.Url.Query;
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {


            if (ViewState["SiteCode"] == null)
            {
                ViewState["SiteCode"] = SiteCode;
                ViewState["VariableCode"] = VariableCode;
                ViewState["URL"] = CurrentURL;
                ViewState["Query"] = Request.Url.Query;
                txtStartdate.Value = DateTime.Parse(BeginDate).ToString(Settings.Default.DateParseFormat);
                txtEndDate.Value = DateTime.Parse(EndDate).ToString(Settings.Default.DateParseFormat);
            }

        }
        protected void Page_Unload(object sender, EventArgs e)
        {

        }

        protected void btnGetData_Click(object sender, EventArgs e)
        {


            Response.Redirect(strPath + "/GetDataCSV.aspx"
                + "?BeginDate=" + txtStartdate.Value
                + "&EndDate=" + txtEndDate.Value
                + "&VariableCode=" + ViewState["VariableCode"]
                + "&SiteCode=" + ViewState["SiteCode"]
                + "&URL=" + ViewState["URL"]);

        }

        protected void btnRePlot_Click(object sender, EventArgs e)
        {

            CultureInfo provider = new CultureInfo(Properties.Settings.Default.Culture);

            String qs = "?BeginDate=" + DateTime.ParseExact(txtStartdate.Value, "d", provider).ToString("s")
                + "&EndDate=" + DateTime.ParseExact(txtEndDate.Value, "d", provider).ToString("s")
                + "&VariableCode=" + ViewState["VariableCode"]
                + "&SiteCode=" + ViewState["SiteCode"]
                + "&URL=" + ViewState["URL"];
            imgChart.ImageUrl = strPathFake + "WaterOneFlowChartStreamer.aspx" + qs;

        }

        public void Page_Error(object sender, EventArgs e)
        {
            Exception objErr = Server.GetLastError().GetBaseException();
            Session["lastError"] = objErr.Message;
            Response.Redirect("ErrorHandler.aspx");
            Server.ClearError();
        }
    }

}